/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver;

import com.sun.faces.application.ActionListenerImpl;
import java.util.logging.Level;
import javax.faces.FacesException;
import javax.faces.component.UICommand;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;

/**
 * The Class JsfActionListener.
 *
 * <p>
 * This action listener monitor jsf actions of a web application.<br/>
 * <br/>
 * Insert into faces-config.xml the code:<br/>
 * <br/>
 * <code>
 * &lt;faces-config&gt;<br/>
 * &nbsp;&lt;application&gt;<br/>
 * 	&nbsp;&nbsp;&lt;action-listener&gt;jeeobserver.JsfActionListener&lt;/action-listener&gt;<br/>
 * &nbsp;&lt;/application&gt;<br/>
 * &lt;faces-config&gt;<br/>
 * </code>
 * </p>
 *
 * @author Luca Mingardi
 * @version 4.0
 */
public class JsfActionListener extends ActionListenerImpl {

	 /**
	  *
	  * @param event
	  *
	  * @throws FacesException
	  */
	 @Override
	 public void processAction(ActionEvent event) throws FacesException {

			final FacesContext facesContext = FacesContext.getCurrentInstance();
			final HttpServletRequest httpServletRequest = (HttpServletRequest) facesContext.getExternalContext().getRequest();

			String projectName = httpServletRequest.getSession().getServletContext().getServletContextName();

			if ((projectName == null) || projectName.isEmpty()) {
				 projectName = httpServletRequest.getContextPath();
				 projectName = projectName.replace("/", "");
			}

			String action = "";

			if (event.getComponent() instanceof UICommand) {
				 final UICommand uiCommand = (UICommand) event.getComponent();
				 if (uiCommand.getActionExpression() != null) {
						action = uiCommand.getActionExpression().getExpressionString();
				 } else {
						action = uiCommand.getClass().getName();
				 }
			} else {
				 JeeObserverContext.logger.log(Level.SEVERE, "Action unknow, component: {0}", event.getComponent().getClass());
			}

			String principal = null;

			try {
				 principal = httpServletRequest.getRemoteUser();
			} catch (final Exception e) {
				 JeeObserverContext.logger.log(Level.FINE, "Unable to get caller principal.");
			}

			final ActionObserver observer = new ActionObserver(ActionObserver.CATEGORY_JSF, projectName, action, principal);
			observer.start();

			try {
				 super.processAction(event);
			} catch (final FacesException exception) {
				 observer.throwException(exception);
				 throw exception;
			} finally {
				 observer.stop();
			}
	 }
}
